package com.kaishengit.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.kaishengit.pojo.Contacts;
import com.kaishengit.pojo.Task;
import com.kaishengit.pojo.TaskType;
import com.kaishengit.pojo.User;
import com.kaishengit.service.TaskService;
import com.kaishengit.util.DateUtil;

public class TaskAction implements SessionAware {
	

	private TaskService taskService;
	private Map<String, Object> session;
	
	private List<Task> todayTaskList;
	private List<Task> outTaskList;
	private List<Task> futureTaskList;
	private List<Contacts> contactsList;
	private List<User> userList;
	private List<TaskType> taskType;
	private Task task;
	private String id;
	private String contactsId;
	private String taskTypeId;
	private String userId;
	private String type;
	private String color;
	
	public String execute() {
		
		todayTaskList = taskService.getTodayTask();
		outTaskList = taskService.getOutTask();
		futureTaskList = taskService.getFutureTask();
		contactsList = taskService.findAllContacts((User)session.get("user"));
		userList = taskService.findAllUser();
		taskType = taskService.findAllTaskType();
		return "success";
	}
	
	
	public String save() {
		task.setContacts(taskService.findContactById(contactsId));
		task.setUser(taskService.findUserById(userId));
		task.setTaskType(taskService.findtaskTypeById(taskTypeId));
		task.setCreateTime(DateUtil.getNowTime());
		task.setComplete(false);
		
		taskService.save(task);
		return "success";
	}
	
	
	public String complete(){
		Task task = taskService.findById(id);
		if(task != null){
			task.setComplete(true);
		}
		taskService.save(task);
		return "success";
	}
	
	public String edit() {
		System.out.println(id);
		task = taskService.findById(id);
		return "success";
	}
	
	public String saveEdit() {
		task.setContacts(taskService.findContactById(contactsId));
		task.setUser(taskService.findUserById(userId));
		task.setTaskType(taskService.findtaskTypeById(taskTypeId));		
		taskService.save(task);
		return "success";
	}
	
	public String newTask() {
		contactsList = taskService.findAllContacts((User)session.get("user"));
		userList = taskService.findAllUser();
		taskType = taskService.findAllTaskType();
		return "success";
	}
	
	
	public String newType() {
		if(id == null){
			TaskType taskType = new TaskType();
			taskType.setType(type);
			taskType.setColor(color);
			taskType.setCreateTime(DateUtil.getNowTime());
			taskService.saveTaskType(taskType);
			id = taskType.getId();
		}else{
			TaskType tt = taskService.findtaskTypeById(id);
			tt.setType(type);
			tt.setColor(color);
			taskService.saveTaskType(tt);
		}
		return "success";
	}
	
	public String newTaskType() {
	    taskType = taskService.findAllTaskType();
		return "success";
	}
	
	public String delType(){
		taskService.delTaskType(id);
		return "success";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	//set get
	
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public void setTodayTaskList(List<Task> todayTaskList) {
		this.todayTaskList = todayTaskList;
	}

	public List<Task> getTodayTaskList() {
		return todayTaskList;
	}

	public void setOutTaskList(List<Task> outTaskList) {
		this.outTaskList = outTaskList;
	}

	public List<Task> getOutTaskList() {
		return outTaskList;
	}

	public void setFutureTaskList(List<Task> futureTaskList) {
		this.futureTaskList = futureTaskList;
	}

	public List<Task> getFutureTaskList() {
		return futureTaskList;
	}

	public List<Contacts> getContactsList() {
		return contactsList;
	}

	public void setContactsList(List<Contacts> contactsList) {
		this.contactsList = contactsList;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}


	public Task getTask() {
		return task;
	}


	public void setTask(Task task) {
		this.task = task;
	}


	public void setId(String id) {
		this.id = id;
	}


	public String getId() {
		return id;
	}


	public void setTaskType(List<TaskType> taskType) {
		this.taskType = taskType;
	}


	public List<TaskType> getTaskType() {
		return taskType;
	}


	public void setContactsId(String contactsId) {
		this.contactsId = contactsId;
	}


	public String getContactsId() {
		return contactsId;
	}


	public void setTaskTypeId(String taskTypeId) {
		this.taskTypeId = taskTypeId;
	}


	public String getTaskTypeId() {
		return taskTypeId;
	}


	public void setUserId(String userId) {
		this.userId = userId;
	}


	public String getUserId() {
		return userId;
	}


	public Map<String, Object> getSession() {
		return session;
	}


	public void setSession(Map<String, Object> session) {
		this.session = session;
	}


	public void setType(String type) {
		this.type = type;
	}


	public String getType() {
		return type;
	}


	public void setColor(String color) {
		this.color = color;
	}


	public String getColor() {
		return color;
	}

	
}
